/*
 * #%L
 * SwiftUIDataAccessLayer
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2011 - 2012 Linkwithweb
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 * #L%
 */
/**
 * 
 */
package com.linkwithweb.products.swiftui.job;

import org.codehaus.plexus.util.StringUtils;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.quartz.QuartzJobBean;

import com.linkwithweb.products.swiftui.context.AsyncDataContainer;
import com.linkwithweb.products.swiftui.context.ExcelFile;
import com.linkwithweb.products.swiftui.context.SystemContext;
import com.linkwithweb.products.swiftui.context.UserContext;
import com.linkwithweb.products.swiftui.service.PlatformService;
import com.linkwithweb.products.swiftui.utils.ServiceLocator;

/**
 * @author ashwin kumar
 * 
 */
public class SwiftUIJobExecutor extends QuartzJobBean {

	@Autowired
	@Qualifier("platformService")
	private PlatformService platformService;

	/**
	 * @return
	 */
	public PlatformService getPlatformService() {
		if (platformService == null) {
			platformService = (PlatformService) ServiceLocator.getService("platformService");
		}
		return platformService;
	}

	public static transient boolean isRunning = false;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.scheduling.quartz.QuartzJobBean#executeInternal(org
	 * .quartz.JobExecutionContext)
	 */
	protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
		if (isRunning) {
			return;
		}

		try {
			isRunning = true;
			// System.out.println("Job Called!!!!!!!!!!!!");
			AsyncDataContainer asyncDataContainer = SystemContext.getInstance().getConcurrentLinkedQueue().poll();
			if (asyncDataContainer != null) {
				SystemContext.getInstance().getClientName().set(asyncDataContainer.getClientId());
				SystemContext.getInstance().resetErrorList();
				SystemContext.getInstance().setUserContext(
						(UserContext) asyncDataContainer.getData().get("userContext"));

				if (StringUtils.equalsIgnoreCase("FILE_UPLOAD", asyncDataContainer.getObjectType())) {
					getPlatformService().loadExcelFile((ExcelFile) asyncDataContainer.getData().get("excelFile"));
				}
			}
		} catch (Throwable e) {
			// TODO: handle exception
		} finally {
			isRunning = false;
		}

	}

}
